package com.example.schooljava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.schooljava.pojo.entity.ClassCurr;
import com.example.schooljava.mapper.ClassCurrMapper;
import com.example.schooljava.mapper.CurriculumMapper;
import com.example.schooljava.pojo.bo.AddClassCurrBO;
import com.example.schooljava.pojo.bo.ClassCurrBO;
import com.example.schooljava.pojo.vo.RelationVO;
import com.example.schooljava.service.ClassCurrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.schooljava.util.HttpStatus;
import com.example.schooljava.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022/08/13
 */
@Service
public class ClassCurrServiceImpl extends ServiceImpl<ClassCurrMapper, ClassCurr> implements ClassCurrService {

    @Autowired
    private ClassCurrMapper classCurrMapper;

    @Autowired
    private CurriculumMapper curriculumMapper;


    /**
     * 功能描述: 查询某班的课程<br>
     * @Param: [classid]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/26 19:37
     */
    @Override
    public Result getCurrOfClass(Long classid) {
        return new Result<>().Huoqu_OK(classCurrMapper.getCurrOfClass(classid));
    }

    /**
     * 功能描述: 查询班级课程关系<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/20 14:51
     */
    @Override
    public Result getRelation(ClassCurrBO classCurrBO) {
        Page<RelationVO> page = new Page<>(classCurrBO.getPage(), classCurrBO.getPageSize());
        return new Result<>().Huoqu_OK(classCurrMapper.getRelation(page,classCurrBO));
    }

    /**
     * 功能描述: 查询所有年级<br>
     * @Param: []
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/22 10:12
     */
    @Override
    public Result getGrade() {
        return new Result<>().Huoqu_OK(classCurrMapper.getGrade());
    }

    /**
     * 功能描述: 查询某年级下所有班级<br>
     * @Param: [grade]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/22 10:13
     */
    @Override
    public Result getClassName(Integer grade) {
        return new Result<>().Huoqu_OK(classCurrMapper.getClassName(grade));
    }

    /**
     * 功能描述: 获取指点年级下的课程<br>
     * @Param: [grade]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/18 11:56
     */
    @Override
    public Result getCurrOfGrade(Integer grade) {
        return new Result<>().Huoqu_OK(classCurrMapper.getCurrOfGrade(grade));
    }

    /**
     * 功能描述: 获取只教指定课程或者没有教课的老师<br>
     * @Param: [curriculum]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/18 11:36
     */
    @Override
    public Result getTeacherOfCurr(String curriculum) {
        // 传入的是id，获取一下课程名
        String name = curriculumMapper.selectById(curriculum).getName();
        return new Result<>().Huoqu_OK(classCurrMapper.getTeacherOfCurr(name));
    }

    /**
     * 功能描述: 添加课程关系<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/18 15:33
     */
    @Override
    public Result addClassCurr(AddClassCurrBO addClassCurrBO) {
        ClassCurr classCurr = new ClassCurr();
        // 条件构造器
        QueryWrapper<ClassCurr> wrapper = new QueryWrapper<>();
        // 根据传入的三个条件判断此课程关系是否已存在
        wrapper.eq("classid",addClassCurrBO.getClassname())
                .eq("currid",addClassCurrBO.getCurriculum());
        if (list(wrapper).size() != 0) return new Result<>().error(false,"添加失败，此关系已存在",HttpStatus.CONFLICT);
        classCurr.setClassid(addClassCurrBO.getClassname());
        classCurr.setCurrid(addClassCurrBO.getCurriculum());
        classCurr.setTeacherid(addClassCurrBO.getTeacher());
        save(classCurr);
        return new Result<>().OK_Back(true,"添加成功！",classCurrMapper.getRelation(new Page<>(1, 10),new ClassCurrBO()));
    }

    /**
     * 功能描述: 删除班级关系<br>
     * @Param: [list]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/18 17:13
     */
    @Override
    public Result delClassCurr(ArrayList<String> list) {
        for (String l : list) {
            for (ClassCurr classCurr : list()) {
                if (Long.parseLong(l) == classCurr.getId())
                    removeById(Long.parseLong(l));
            }
        }
        return new Result<>().OK_Back(true,"删除成功！",classCurrMapper.getRelation(new Page<>(1, 10),new ClassCurrBO()));
    }

    /**
     * 功能描述: 修改班级信息<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/18 17:17
     */
    @Override
    public Result editClassCurr(AddClassCurrBO addClassCurrBO) {
        // 条件构造器
        QueryWrapper<ClassCurr> wrapper = new QueryWrapper<>();
        // 根据传入的三个条件判断此课程关系是否已存在
        wrapper.eq("currid",addClassCurrBO.getCurriculum())
                .eq("id",addClassCurrBO.getId())
                .eq("teacherid",addClassCurrBO.getTeacher());
        if (list(wrapper).size() != 0) return new Result<>().error("","修改失败，此关系已存在",HttpStatus.CONFLICT);
        // 修改注入
        ClassCurr classCurr = new ClassCurr();
        classCurr.setId(addClassCurrBO.getId());
        classCurr.setTeacherid(addClassCurrBO.getTeacher());
        classCurr.setCurrid(addClassCurrBO.getClassname());
        updateById(classCurr);
        return new Result<>().OK_Back(true,"修改成功！",classCurrMapper.getRelation(new Page<>(1, 10),new ClassCurrBO()));
    }
}
